package com.it.travel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.travel.dao.CategoryMapper;
import com.it.travel.dao.ProductMapper;
import com.it.travel.dao.StoryMapper;
import com.it.travel.pojo.Category;
import com.it.travel.pojo.Story;
import com.it.travel.service.IndexService;
import com.it.travel.vo.IndexDestinationVO;
import com.it.travel.vo.IndexStoryVO;
import com.it.travel.vo.Params.IndexParams;
import com.it.travel.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private ProductMapper productMapper;


    @Resource
    private StoryMapper storyMapper;

    @Override
    public Result<?> getInfo(IndexParams indexParams) {
        String destination = indexParams.getDestination();
        String hotel = indexParams.getHotel();
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Category> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(Category::getName, "地点");
        Category category = categoryMapper.selectOne(wrapper1);
        LambdaQueryWrapper<Category> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(Category::getParentId, category.getId());
//        wrapper2.orderByDesc(Category::getCreateTime);
        wrapper2.last("limit 3");
        List<Category> cityList = categoryMapper.selectList(wrapper2);
        LambdaQueryWrapper<Story> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.last("limit 4");
        List<Story> storyListAll = storyMapper.selectList(wrapper3);
       List<IndexStoryVO> storyList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Story story : storyListAll) {
            IndexStoryVO indexStoryVO = new IndexStoryVO();
            String description = story.getDescription();
            String shortStr=description;
            if (StringUtils.isNotBlank(description)&&description.length()>=70){
                shortStr=shortStr.substring(0,68)+"......";
            }
            BeanUtil.copyProperties(story,indexStoryVO);
            indexStoryVO.setDescription(shortStr);
            String format = simpleDateFormat.format(new Date(story.getCreateTime()));
            indexStoryVO.setFormatTime(format);
            storyList.add(indexStoryVO);
        }
        List<IndexDestinationVO> indexDestinationVOList = productMapper.queryIndexDestinationInfoByCategoryName(destination,0,3);
        List<IndexDestinationVO> indexDestinationVOList1 = productMapper.queryIndexDestinationInfoByCategoryName(hotel,0,4);
        map.put("storyList",storyList);
        map.put("cityList", cityList);  // 首页的城市数据
        map.put("destincationList", indexDestinationVOList);  // 首页的目的地数据
        map.put("hotelList", indexDestinationVOList1);  // 首页的酒店数据
//        storyList.forEach(System.out::println);
        return Result.success(map, "获取首页数据成功");
    }
}
